<template>
	<view class="">
		<view class="wrap">

			<uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules">
				<view class="dis" style="justify-content: space-between;align-items: baseline;">
					<uni-forms-item label="手机号" required name="phone" style="width: 70%;" :labelWidth='90'>
						<uni-easyinput v-model="baseFormData.phone" placeholder="请输入手机号" />

					</uni-forms-item>
					<view class="getCode" v-if='countdown==0' @click="getRegisterVerify(1)">获取验证码</view>
					<view class="getCode" v-else @click="getRegisterVerify(2)">{{countdown}}秒</view>

				</view>


				<uni-forms-item label="验证码" required name="verCode" :labelWidth='90'>
					<uni-easyinput v-model="baseFormData.verCode" placeholder="请输入验证码" />
				</uni-forms-item>
				<uni-forms-item label="登录密码" required name="pwd" :labelWidth='90'>
					<uni-easyinput v-model="baseFormData.pwd" placeholder="请输入登录密码" />

				</uni-forms-item>
				<uni-forms-item label="确认密码" required name="passWordSubmit" :labelWidth='90'>
					<uni-easyinput v-model="baseFormData.passWordSubmit" placeholder="请确认密码" />
				</uni-forms-item>

				<uni-forms-item label="联系人姓名" required name="contactName" :labelWidth='90'>
					<uni-easyinput v-model="baseFormData.contactName" placeholder="请输入联系人姓名" />
				</uni-forms-item>

				<uni-forms-item label="选择省市区" required name="citys" :labelWidth='90'>

					<uni-easyinput @focus='openPicker' v-model="baseFormData.citys" placeholder="选择省市区" />
				</uni-forms-item>

				<template v-if="loginType==2">
					<uni-forms-item label="公司名称" required name="complyName" :labelWidth='90'>
						<uni-easyinput v-model="baseFormData.complyName" placeholder="请输入公司名称" />

					</uni-forms-item>



				</template>

				<template v-if="loginType==3">
					<uni-forms-item label="党支部名称" required name="unitName" :labelWidth='90'>
						<uni-easyinput v-model="baseFormData.unitName" placeholder="请输入党支部名称" />
					</uni-forms-item>
				</template>
				<uni-forms-item label="详细地址" required name="address" :labelWidth='90'>
					<uni-easyinput v-model="baseFormData.address" placeholder="请输入详细地址" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="ding">

		</view>
		<view class="footer">
			<view class="footer-btn" @click="submit">
				提交
			</view>

		</view>
		<cc-selectDity :province="province" :city="city" :area="area" :show="show" @changeClick="changeClick"
			@sureSelectArea="onsetCity" @hideShow="onhideShow"></cc-selectDity>
	</view>
</template>

<script>
	import {
		registerVerify,
		register
	} from '@/api/user.js'
	export default {
		data() {
			return {
				colors: '#f23a3a',
				show: false,
				province: "浙江省",
				city: "杭州市",
				area: "萧山区",
				addressData: {
					name: '',
					phone: '',
					address: '',
					moreAddres: '',
					isdefult: 0
				},
				isShow: true,



				rules: {

					phone: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},

					verCode: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},
					unitName: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},


					pwd: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},
					passWordSubmit: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},


					contactName: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},
					citys: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},

					complyName: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},

					address: {
						rules: [{
							required: true,
							errorMessage: '必填',
						}]
					},

				},
				baseFormData: {
					phone: '',
					verCode: '',
					pwd: '',
					contactName: '',
					citys: '',
					complyName: '',
					address: '',
					passWordSubmit: '',

					unitName: ''
				},
				loginType: 0,
				countdown: 0
			}
		},
		onLoad(e) {
			this.loginType = e.type


			console.log(e, 'eeeeeeeeeeeee')
		},
		methods: {
			startCountdown() {
				this.countdown = 60; // 设置倒计时初始值
				const timer = setInterval(() => {
					if (this.countdown > 0) {
						this.countdown--; // 每秒减1
					} else {
						clearInterval(timer); // 倒计时结束，清除定时器
					}
				}, 1000);
			},
			getRegisterVerify(type) {
				if (type == 1) {
					registerVerify(this.baseFormData.phone).then(res => {
						console.log("res", res)
						uni.showToast({
							title: res.message,
							duration: 2000,

						})
						this.startCountdown()
					})
				} else {
					uni.showToast({
						title: '请' + this.countdown + '秒后在获取',
						duration: 2000,
						icon: 'none'
					})
				}

			},
			openPicker() {
				console.log('执行打开地址选择器')
				this.show = true
			},

			changeClick(value, value2, value3) {
				this.province = value;
				this.city = value2;
				this.area = value3;
				console.log("222")
				this.baseFormData.citys = value + value2 + value3

				console.log('地址选择器 = ' + value + value2 + value3);



			},
			onhideShow() {
				this.show = false
				console.log('执行了关闭地址选择器')
			},
			//选中省市区
			onsetCity(e) {
				console.log("999", e)
				let data = e.detail.target.dataset;
				this.province = data.province
				this.city = data.city
				this.area = data.area

				let address = data.province + data.city + data.area;
				this.baseFormData.citys = address
				this.show = false
				this.addressData.address = address
			},
			// 
			switchChange(e) {

			},
			submit() {
				// console.log(this.province, this.city, this.area)
				// province: "浙江省",
				// city: "杭州市",
				// area: "萧山区",
				// uni.navigateTo({
				// 	url: "/pages/users/wechat_login/index"
				// })
				this.$refs.baseForm.validate().then(res => {
					res.province = this.province
					res.city = this.city
					res.area = this.area
					if (res.passWordSubmit != res.pwd) {
						uni.showToast({
							title: "密码不一致，请重新输入",
							icon: 'none'
						})
						return;
					}
					if (this.loginType == 3) {
						res.tag = "党建用户"
					} else if (this.loginType == 2) {
						res.tag = "企业用户"
					}
					register(res).then(ress => {

						uni.showToast({
							title: "申请成功",
							duration: 2000
						})
						setTimeout(() => {
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.account = this.baseFormData.phone; //修改上一页data里面的地址
							uni.navigateBack({
								delta: 1 //返回层数，2则上上页
							});
						}, 2000)
					})
					console.log('表单数据信息：', res);
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.getCode {
		width: 28%;
		text-align: right;
		font-size: 24rpx;
		color: $maincolor;
	}

	.footer {
		width: 100%;
		position: fixed;
		bottom: 0;
		background: #fff;
		z-index: 1000;
		padding: 30rpx 0;

		.footer-btn {
			width: 90%;
			margin: 0 auto;
			font-size: 28rpx;
			line-height: 80rpx;
			border-radius: 100rpx;
			background: $maincolor;
			text-align: center;
			color: #fff;
		}
	}

	.wrap {
		padding: 30rpx;
		background: #fff;
	}
</style>